package com.yelobblog.yelobblog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yelobblog.yelobblog.common.exception.ApiException;
import com.yelobblog.yelobblog.common.exception.ErrorEnum;
import com.yelobblog.yelobblog.common.untils.ThreadLocalUtil;
import com.yelobblog.yelobblog.common.untils.userStatusEnum;
import com.yelobblog.yelobblog.domain.Vo.commentsVo;
import com.yelobblog.yelobblog.service.commentService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.yelobblog.yelobblog.mapper.commentsMapper;
import com.yelobblog.yelobblog.domain.entity.comments;
import com.yelobblog.yelobblog.domain.Dto.commentsDto;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class commentServiceimpl extends ServiceImpl<commentsMapper,comments> implements commentService {
    @Resource
    private commentsMapper commentsMapper;
    @Override
    @Transactional
    public void addComment(commentsDto commentsDto) {
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        comments comments = new comments();
        BeanUtils.copyProperties(commentsDto,comments);
        comments.setUserId(userId);
        commentsMapper.insert(comments);
        commentsMapper.addCommentCount(comments.getArticleId());
    }
    @Override
    @Transactional
    public void deleteComment(Integer id){
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        Integer role = (Integer) claims.get("role");
        if(!role.equals(userStatusEnum.SUPERUSER.getCode())) {
            if (!commentsMapper.selectById(id).getUserId().equals(userId)) {
                throw new ApiException(ErrorEnum.PERMISSION_DENIED.getErrorCode(), ErrorEnum.PERMISSION_DENIED.getErrorMsg());
            }
        }
        commentsMapper.updateIsDelete(id,true);
        commentsMapper.deleteCommentCount(commentsMapper.selectById(id).getArticleId());
    }
    @Override
    public Page<commentsVo> getCommentsByArticleId(Integer pageNum, Integer pageSize, Integer articleId){
        return commentsMapper.getCommentsByArticleId(new Page<>(pageNum,pageSize),articleId);
    }
    @Override
    public Page<commentsVo> getCommentsByrootCommentsId(Integer pageNum, Integer pageSize, Integer rootCommentsId){
        return commentsMapper.getSubCommentsByRootCommentsId(new Page<>(pageNum,pageSize),rootCommentsId);
    }
    @Transactional
    @Override
    public void likeComment(Integer id){
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        commentsMapper.addLikeCount(id);
        commentsMapper.addCommentLike(userId,id);
    }
    @Transactional
    @Override
    public void dislikeComment(Integer id){
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        commentsMapper.deleteCommentLike(userId,id);
        commentsMapper.deleteLikeCount(id);
    }
    @Override
    public Boolean getCommentLike(Integer commentId){
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        return commentsMapper.getCommentLike(userId,commentId)!=null;
    }
}
